<?php
require_once("function/function.php");
require_once("template/header.php");
error_reporting(E_ERROR);


$post = safe_text($_GET[post]);
$angajator = safe_text($_GET[angajator]);
$judet = safe_text($_GET[judet]);
$page = (!empty($_GET[page])) ? $_GET[page] : 1;
$perpage = 10;
?>
<script>
  $(document).ready(function(){
    initialize_map_small();
	populate_small_map();
  });
</script>	
	<div class="row">
    	<div class="span5">
        	<h3>Detalii cautare</h3>
            <table class="table table-hover"  >
           <tr><td><strong>Post  </strong></td><td><?php echo (empty($post))? "Orice post" :$post."<br>"; ?></td></tr>
       		<tr><td><strong>Angajatori  </strong></td><td><?php echo ((empty($angajator))? "Orice angajator" :$angajator)."<br>"; ?></td></tr>
            <tr><td><strong>Judet  </strong></td><td><?php echo ((empty($judet))? "Orice judet" :$judet)."<br>"; ?></td></tr></table>
        </div>
        	<div class="span2">

       		 <div id="map_canvas_small" class="hidden-phone" style="width:100%;margin-left:1%; height:150px; margin-top:30px; background-color:#ddd;"></div>    </div>
        	</div>
   

					
<?php



//$req = "termenlimitadepuneredosare lt datetime'".date('Y-m-d')."'"; 
$req = "tip ne ''";
if(!empty($post))
	$req  .= "and tip eq '".$post."'";
	

if(!empty($angajator))
	$req .= " and angajator eq '".$angajator."'";
//	$req .= " and substringof('".$angajator."',angajator) eq true";
	
if(!empty($judet))
	$req .= " and judet eq '".$judet."'";

$limit = $page*$perpage+1;


$url = 'http://rogovdata.cloudapp.net:8080/v1/RoGovOpenData/Posturi/?$select=tip,angajator,tipangajator,judet,localitate,entityid&$filter='.rawurlencode($req).'&format=json&$top='.$limit;

//echo $url;

  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  
 // echo $data;
  
 $data_array_original = json_decode($data,true);
 

 $data_array = array_slice($data_array_original['d'],($page-1)*$perpage,$perpage);
 $res=array();
 foreach ($data_array as $val) {
 	$res[]=$val;
 }
 echo '<span id="jsonData" style="display:none;">'.json_encode($res).'</span>';
 
 ///PAGINARE
 $baseurl = 'cautare.php?post='.$post.'&angajatori='.$angajator.'&judet='.$judet;
 
 if($page > 1)
	$paginare .= '<a href="'.$baseurl.'&page='.($page-1).'" class="btn btn-primary pull-left">Pagina anterioara</a>'; 

if(count($data_array_original['d']) == $limit)
	$paginare .= '<a href="'.$baseurl.'&page='.($page+1).'" class="btn btn-primary pull-right">Pagina urmatoare</a>';
	 
	 
 
 
 if(count($data_array) > 0){
 
 ///PAGINARE
 
 echo "<br>".$paginare;
 echo "<table class='table table-striped'>";
 echo "<thead><td>#</td><td>Post</td><td>Angajator</td><td>Tip angajator</td><td>Judet</td><td>Localitate</td><td width='80px'></td></thead>";
 $k = ($page-1)*$perpage+1;
  foreach($data_array as $job){
	echo "<tr>";
	echo "<td>".$k."</td>";
	echo "<td><strong>".$job[tip]."</strong></td>";
	echo "<td>".$job[angajator]."</td>";
	echo "<td>".$job[tipangajator]."</td>";
	echo "<td>".$job[judet]."</td>";
	echo "<td>".$job[localitate]."</td>";
	echo "<td><a href='post.php?entityid=".$job[entityid]."' class='btn btn-success btn-small'>Vezi post</a></td>";
	echo "</tr>";
	 
	 $k++;
 }
 echo "</table>";
 echo $paginare;

 
 }else{
	echo "<h3>Nu am gasit rezultate</h3>";
 }
  ?>

<?php

require_once("template/footer.php");
?>